package edu.controller;

import edu.service.ClassService;
import edu.vo.Class;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/classes")
public class ClassController {

    @Autowired
    private ClassService classService;

    /**
     * 保存班级信息
     * @param clazz 班级对象
     * @return 保存后的班级对象
     */
    @PostMapping
    public ResponseEntity<Class> saveClass(@RequestBody Class clazz) {
        Class savedClass = classService.saveClass(clazz);
        return new ResponseEntity<>(savedClass, HttpStatus.CREATED);
    }

    /**
     * 根据 ID 获取班级信息
     * @param id 班级 ID
     * @return 班级对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<Class> getClassById(@PathVariable Integer id) {
        Optional<Class> classOptional = classService.getClassById(id);
        return classOptional.map(clazz -> new ResponseEntity<>(clazz, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * 获取所有班级信息
     * @return 班级列表
     */
    @GetMapping
    public ResponseEntity<List<Class>> getAllClasses() {
        List<Class> classes = classService.getAllClasses();
        return new ResponseEntity<>(classes, HttpStatus.OK);
    }

    /**
     * 根据 ID 删除班级信息
     * @param id 班级 ID
     * @return 无内容响应
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteClassById(@PathVariable Integer id) {
        classService.deleteClassById(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}